﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Platformer1
{
    class Unicorn
    {
        public bool bActive = true;
        public Vector2 Position;
        public Vector2 Velocity;
        protected Animation runAnimation;
        protected Animation run2Animation;
        protected SpriteEffects flip = SpriteEffects.None;
        protected AnimationPlayer sprite;
        public Level Level
        {
            get { return level; }
            set { level = value; }
        }
        Level level;

        public Unicorn(Level level, Vector2 position, Vector2 velocity)
        {
            this.level = level;
            this.Position = position;
            this.Velocity = velocity;

            LoadContent();
        }



        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Position += (Velocity * elapsed);


            if (Position.X + runAnimation.FrameWidth >= level.Width * Tile.Width)
            {
                level.unicorn = null;
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (level.Players.Length > 1)
            {
                sprite.PlayAnimation(run2Animation);
            }
            else
            {
                sprite.PlayAnimation(runAnimation);
            }
            // Flip the sprite to face the way we are moving.
            if (Velocity.X < 0)
                flip = SpriteEffects.FlipHorizontally;
            else
                flip = SpriteEffects.None;


            // Draw that sprite.
            sprite.Draw(gameTime, spriteBatch, Position, flip);
        }

        public void LoadContent()
        {
            runAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Unicorn/Run"), 0.1f, true);
            run2Animation = new Animation(Level.Content.Load<Texture2D>("Sprites/Unicorn/Run2"), 0.1f, true);
        }
    }
}
